using Gateway.Common.Enums;
using Gateway.IQuery.Language;
using Gateway.IQuery.Language.Result;
using Gateway.IRepositories;
using SqlSugar;

namespace Gateway.Query;

/// <summary>
/// 运行语言查询
/// </summary>
public class LanguageQuery : ILanguageQuery
{
    private readonly IQueryRepository _repository;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="repository"></param>
    public LanguageQuery(IQueryRepository repository)
    {
        _repository = repository;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public async Task<List<LanguageDetailResult>> GetAllList()
    {
        return await _repository.QueryListAsync<LanguageDetailResult>(@"select * from  Language where Deleted='N' ");
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public async Task<List<LanguageSimpleResult>> GetAvailableList()
    {
        return await _repository.QueryListAsync<LanguageSimpleResult>(
            @"select * from Language where Deleted='N' and Disabled='N'");
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="lang"></param>
    /// <returns></returns>
    public async Task<LanguageDetailResult> GetDetailByLang(string lang)
    {
        return await _repository.QueryFirstOrDefaultAsync<LanguageDetailResult>(
            @"select * from Language where Deleted='N' and Lang=@lang", new { lang });
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="languageId"></param>
    /// <returns></returns>
    public async Task<List<LanguageExtensionCodeDetailResult>> GetCodes(string languageId)
    {
        var data = await _repository.GetListAsync<LanguageExtensionCodeDetailResult>(p =>
            p.LanguageId.Equals(languageId)
            && p.Deleted == BooleanStatusEnum.No.Value
        );
        return data;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="language"></param>
    /// <returns></returns>
    public async Task<List<LanguageExtensionCodeDetailResult>> GetCodesByLang(string language)
    {
        var data = await _repository.Db.Queryable<LanguageExtensionCodeDetailResult, LanguageDetailResult>(
                (le, m) => new JoinQueryInfos(
                    JoinType.Inner, le.LanguageId == m.LanguageId
                )
            )
            .Where((le, m) => m.Lang.Equals(language))
            .ToListAsync();
        return data;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    public async Task<List<LanguageExtensionCodeDetailResult>> GetLangExtensionCodeByIds(List<string> ids)
    {
        var data = await _repository.GetListAsync<LanguageExtensionCodeDetailResult>(x =>
            ids.Contains(x.LanguageExtensionCodeId));
        return data;
    }
}